package com.activititest.controller;

import com.activititest.appentity.Response;
import com.activititest.entity.*;
import com.activititest.service.*;
import com.activititest.service.impl.CommonServerImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//
//
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private CommonServerImpl commonServerImpl;
    @Autowired
    private IVHiTaskResaltService iVHiTaskResaltService;

    @Autowired
    private IVEmEnterperispolltypeService iVEmEnterperispolltypeService;

    @PostMapping("/addgroup/")
    public String addGroup(@RequestBody EGroup eGroup) {
        boolean b = commonServerImpl.addGroup(eGroup);
        return "yes";
    }

    @PostMapping("/creatuser/")
    public String creatUser(@RequestBody EUser euser) {
        boolean b = commonServerImpl.addUser(euser);
        return "yes";
    }

    @PostMapping("/login/")
    public Response login(@RequestBody EUser euser) {
        System.out.println(euser.getUsername() + "---------------------------------------");
        EUser loginuser = commonServerImpl.login(euser.getUsername(), euser.getPassword());

        Response euserResponse = new Response();
        euserResponse.setCode(200);

        euserResponse.setData(loginuser);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //获取企业污染源类型
    @PostMapping("/listenterperisptype/")
    public Response<List<VEmEnterperispolltype>> listEnterperisptype(@RequestParam("enterpriseid") String enterpriseid) {
        QueryWrapper<VEmEnterperispolltype> queryWrapper = new QueryWrapper<VEmEnterperispolltype>().eq("enterprise_id", "202");
        List<VEmEnterperispolltype> list = iVEmEnterperispolltypeService.list(queryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //获取企业历史数据
    @PostMapping("/enterperishisinfor/")
    public Response<List<VHiTaskResalt>> enterperishisinfor(@RequestParam("enterpriseid") String enterpriseid) {
        QueryWrapper<VHiTaskResalt> enterprise_id = new QueryWrapper<VHiTaskResalt>().eq("enterprise_id", enterpriseid);
        List<VHiTaskResalt> list = iVHiTaskResaltService.list();
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //区域应急能力查询
    @Autowired
    private IVEmInstrumentListService iVEmInstrumentListService;
    @Autowired
    private IVProvinceMonitorService ivProvinceMonitorService;

    @PostMapping("/getinstrumentlist/")
    public <T> Response<List<T>> getInstrumentList(@RequestParam("areaid") String areaid) {
        Response euserResponse = new Response();
        if ("210000".equals(areaid)) {
            QueryWrapper<VProvinceMonitor> vEmInstrumentListQueryWrapper = new QueryWrapper<>();
            vEmInstrumentListQueryWrapper.eq("instrument_city_id", areaid);
            List<VProvinceMonitor> list = ivProvinceMonitorService.list(vEmInstrumentListQueryWrapper);
            euserResponse.setData(list);
        } else {
            QueryWrapper<VEmInstrumentList> vEmInstrumentListQueryWrapper = new QueryWrapper<>();
            vEmInstrumentListQueryWrapper.eq("instrument_city_id", areaid);
            List<VEmInstrumentList> list = iVEmInstrumentListService.list(vEmInstrumentListQueryWrapper);
            euserResponse.setData(list);
        }

        euserResponse.setCode(200);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //实验室仪器
    @Autowired
    private IVProvinceLaboratoryService ivProvinceLaboratoryService;

    @PostMapping("/getProvinceLaboratorylist/")
    public Response<List<VProvinceLaboratory>> getProvinceLaboratorylist(@RequestParam("areaid") String areaid) {
        QueryWrapper<VProvinceLaboratory> vEmInstrumentListQueryWrapper = new QueryWrapper<>();
        vEmInstrumentListQueryWrapper.eq("instrument_city_id", areaid);
        List<VProvinceLaboratory> list = ivProvinceLaboratoryService.list(vEmInstrumentListQueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //防护及通讯设备
    @Autowired
    private IVProvinceProtectcommunicationService ivProvinceProtectcommunicationService;

    @PostMapping("/getProvinceProtection/")
    public <T> Response<List<T>> getProvinceProtectionlist(@RequestParam("areaid") String areaid) {
        QueryWrapper<VProvinceProtectcommunication> Wrapper = new QueryWrapper<>();
        Wrapper.eq("instrument_city_id", areaid);
        List<VProvinceProtectcommunication> list = ivProvinceProtectcommunicationService.list(Wrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }


    //自助站查询
    @Autowired
    private ISiteInfoService iSiteInfoService;

    @PostMapping("/getSiteInfo/")
    public Response<List<SiteInfo>> getSiteInfo() {
//        SiteInfo_PollutionRealnew siteInfo_pollutionRealnew = new SiteInfo_PollutionRealnew();
        List<SiteInfo> siteInfoList = iSiteInfoService.list();
//        List<PollutionRealNew> pollutionRealNewList = iPollutionRealNewService.list();
//        siteInfo_pollutionRealnew.setSiteInfoList(siteInfoList);
//        siteInfo_pollutionRealnew.setPollutionRealNewList(pollutionRealNewList);
        Response infoResponse = new Response();
        infoResponse.setCode(200);
        infoResponse.setData(siteInfoList);
        infoResponse.setMsg("yes");
        return infoResponse;

    }

    @PostMapping("/getSiteInfo_PollutionReal/")
    public Response<SiteInfo_PollutionRealnew> getSiteInfo_PollutionReal() {
        SiteInfo_PollutionRealnew siteInfo_pollutionRealnew = new SiteInfo_PollutionRealnew();
        List<SiteInfo> siteInfoList = iSiteInfoService.list();
        List<PollutionRealNew> pollutionRealNewList = iPollutionRealNewService.list();
        siteInfo_pollutionRealnew.setSiteInfoList(siteInfoList);
        siteInfo_pollutionRealnew.setPollutionRealNewList(pollutionRealNewList);
        Response infoResponse = new Response();
        infoResponse.setCode(200);
        infoResponse.setData(siteInfo_pollutionRealnew);
        infoResponse.setMsg("yes");
        return infoResponse;

    }

    //自动站实时监测查询
    @Autowired
    private IPollutionRealNewService iPollutionRealNewService;

    @PostMapping("/getIPollutionRealNew/")
    //public Response<List<PollutionRealNew>> getIPollutionRealNew(@RequestParam("site_id")String site_id){
    public Response<List<PollutionRealNew>> getIPollutionRealNew() {
        QueryWrapper<PollutionRealNew> pollutionRealNewQueryWrapper = new QueryWrapper<>();

        List<PollutionRealNew> list = iPollutionRealNewService.list(pollutionRealNewQueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //自动站历史监测查询
    @Autowired
    private IPollutionRealService iPollutionRealService;

    @PostMapping("/getPollutionHistory/")
    public Response<List<PollutionReal>> getPollutionHistory(@RequestParam("site_name") String site_name, @RequestParam("pollution_name") String pollution_name, @RequestParam("enter_time") String enter_time) {
        QueryWrapper<PollutionReal> pollutionRealHistoryQueryWrapper = new QueryWrapper<>();
        pollutionRealHistoryQueryWrapper.eq("site_name", site_name)
                .eq("pollution_name", pollution_name)
                .apply("CONVERT(varchar(100), enter_time, 23)= '" + enter_time + "'");
        List<PollutionReal> list = iPollutionRealService.list(pollutionRealHistoryQueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //通过自动站名称查询自动站实时监测查询
    @Autowired
    private IPollutionRealNewService iPollutionRealNewService1;

    @PostMapping("/getIPollutionRealNew1/")
    public Response<List<PollutionRealNew>> getIPollutionRealNew1(@RequestParam("site_name") String site_name) {
        QueryWrapper<PollutionRealNew> pollutionRealNew1QueryWrapper = new QueryWrapper<>();
        pollutionRealNew1QueryWrapper.eq("site_name", site_name);
        List<PollutionRealNew> list = iPollutionRealNewService1.list(pollutionRealNew1QueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    //通过自动站名称查询自动站实时监测查询
    @Autowired
    private IActIdGroupService iActIdGroupService;

    @PostMapping("/getidGroup/")
    public Response<List<ActIdGroup>> getActId() {
        QueryWrapper<ActIdGroup> getActIdQueryWrapper = new QueryWrapper<>();

        List<ActIdGroup> list = iActIdGroupService.list(getActIdQueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }
//    查询是否更新
    @Autowired
    private IAppVersionService iAppVersionService;

    @PostMapping("/update/")
    public Response<List<AppVersion>> update() {
        QueryWrapper<AppVersion> updateQueryWrapper = new QueryWrapper<>();
        updateQueryWrapper.inSql("id", "SELECT MAX (id)  from APP_VERSION where id > 0");
        List<AppVersion> list = iAppVersionService.list(updateQueryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }


}
